﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCSLibrary;
using System.Data;
using Utils.DC.Web.UI;
using Utils;

public partial class Admin_Form_Adv_Add : System.Web.UI.Page
{
    AdvUI objAdv = new AdvUI();
    DataSet dsAdv;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Trim() != "")
            {
                try
                {
                    LoadEditData(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch (FormatException fe)
                {
                }
            }
            cboAdvLocation_Bind();
            cboAdvType_Bind();
        }
    }

    private void cboAdvLocation_Bind()
    {
        PropertiesUI objPro = new PropertiesUI();
        DataSet ds = objPro.GetByPropertyTypeName("AdvLocation");
        if (ds != null && ds.Tables.Count > 0)
        {
            cboAdvLocation.DataSource = ds.Tables[0];
            cboAdvLocation.DataBind();
        }
    }

    private void cboAdvType_Bind()
    {
        PropertiesUI objPro = new PropertiesUI();
        DataSet ds = objPro.GetByPropertyTypeName("AdvType");
        if (ds != null && ds.Tables.Count > 0)
        {
            cboAdvType.DataSource = ds.Tables[0];
            cboAdvType.DataBind();
        }
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        //bien page-alias
        if (txtAdvID.Value.Trim() == "")
        {
            try
            {
                //int result = objAdv.Add(1,txtCompanyName.Text.Trim(), cboAdvLocation.SelectedValue,txtAdvSource.Text,"",  txtAdvLink.Text.Trim(), Convert.ToInt32(txtViewOrder.Text.Trim()),"",cboAdvType.SelectedValue,chkIsApproved.Checked,"","");
                int result = objAdv.Add(1, txtCompanyName.Text.Trim(), cboAdvLocation.SelectedValue, txtAdvSource.Text + "/", txtAdvDescription.Text.Trim(), Format.ConvertToNoSign(txtCompanyName.Text.Trim()), Convert.ToInt32(txtViewOrder.Text.Trim()), "", cboAdvType.SelectedValue, chkIsApproved.Checked, "", "");
                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                //lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
                lblMessage.Text = ex.ToString();
            }
        }
        else
        {
            try
            {

                int result = objAdv.Update(Convert.ToInt32(txtAdvID.Value), 1, txtCompanyName.Text.Trim(), cboAdvLocation.SelectedValue, txtAdvSource.Text.Trim() + "/", txtAdvDescription.Text.Trim(), Format.ConvertToNoSign(txtCompanyName.Text.Trim()), Convert.ToInt32(txtViewOrder.Text.Trim()), "", cboAdvType.SelectedValue, chkIsApproved.Checked, "", "");

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
            }
        }
    }


  
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/quan-tri-noi-dung/quan-tri-quang-cao.aspx");
    }

    #region Cac ham tuong tac du lieu

    private void LoadEditData(int AdvID)
    {
        DataSet ds = objAdv.GetByID(AdvID);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            txtAdvID.Value = row["AdvID"].ToString();
            cboAdvType.SelectedValue = row["AdvType"].ToString();
            txtCompanyName.Text = row["CompanyName"].ToString();
            txtAdvSource.Text = row["AdvSource"].ToString();
            txtAdvDescription.Text = row["AdvDescription"].ToString();
            chkIsApproved.Checked = Convert.ToBoolean(row["IsApproved"]);
            txtViewOrder.Text = row["ViewOrder"].ToString();
            btnCreate.Text = "Cập nhật";
        }
    }

    private void ReInsertData()
    {
        //txtAdvID.Value = "";
        txtCompanyName.Text = "";
        txtAdvDescription.Text = "";
        chkIsApproved.Checked = false;
        txtViewOrder.Text = "";
    }

    #endregion
}
